package com.example.tse.rbhttpservice.service.group;


import android.content.Context;

import com.example.tse.rbhttpservice.service.HttpService;
import com.example.tse.rbhttpservice.service.RemoteServiceListenerT;
import com.example.tse.rbhttpservice.service.ResponseServiceListenerT;
import com.example.tse.rbhttpservice.service.RudderResponse;
import com.example.tse.rbhttpservice.service.group.model.GroupGetByCodeRequest;
import com.example.tse.rbhttpservice.service.group.model.GroupGetByCodeResponse;
import com.example.tse.rbhttpservice.service.group.model.GroupGetList;
import com.example.tse.rbhttpservice.service.group.model.GroupGetListRequest;
import com.example.tse.rbhttpservice.service.group.model.GroupGetListResponse;
import com.example.tse.rbhttpservice.service.group.model.GroupGetRequest;
import com.example.tse.rbhttpservice.service.group.model.GroupGetResponse;
import com.example.tse.rbhttpservice.service.group.model.GroupJoinRequest;
import com.example.tse.rbhttpservice.service.group.model.GroupJoinResponse;
import com.example.tse.rbhttpservice.service.group.model.GroupOpenRequest;
import com.example.tse.rbhttpservice.service.group.model.GroupOpenResponse;
import com.example.tse.rbhttpservice.service.group.model.GroupQuitRequest;
import com.example.tse.rbhttpservice.service.group.model.GroupQuitResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.kymjs.kjframe.http.HttpParams;

import java.lang.reflect.Type;
import java.util.List;


/**
 * 
 * @author wliu
 *    edi  tse
 */
public class GroupServiceImpl implements GroupService {
	private Context context;
	private static Gson gson = new Gson();
	
	
	public GroupServiceImpl(Context context) {
		this.context = context;
	}
	/**
	 * 获取团购列表，仅获取开始中的团购数据，如果用户已参与某团，按钮“我要开团”变成“我已参团”
	 * @param request
	 * @param listener
	 * @return GroupGetListResponse
	 */		
	 
	    public void getList(GroupGetListRequest request, final RemoteServiceListenerT<GroupGetListResponse> listener) {

        final HttpParams params = new HttpParams();
        


        final String url = "group/getList";


        
        /**
        group
        getList
        GetList
        获取团购列表，仅获取开始中的团购数据，如果用户已参与某团，按钮“我要开团”变成“我已参团”
        GET
        Group
        GroupGetList
        group/getList
        array
        */
	
           final Type type = new TypeToken<List<GroupGetList>>() {
        }.getType();



            new HttpService().getList(context, url, params, GroupGetListResponse.class, new ResponseServiceListenerT<RudderResponse>() {

             @Override
             public void onPreStart() {
                 super.onPreStart();
                 listener.PreStart();
             }

             @Override
             public void ok(RudderResponse rudderResponse) {
                 super.ok(rudderResponse);
                 List<GroupGetList> items = gson.fromJson(rudderResponse.getBody(), type);
                 GroupGetListResponse responce = new GroupGetListResponse();
                 responce.setGetList(items);
                 listener.ok(responce);
             }

             @Override
             public void okData(RudderResponse rudderResponse) {
                 super.okData(rudderResponse);

                 if (rudderResponse == null) {
                     listener.okData(new GroupGetListResponse());
                 } else {
                     List<GroupGetList> items = gson.fromJson(rudderResponse.getBody(), type);
                     GroupGetListResponse responce = new GroupGetListResponse();
                     responce.setGetList(items);
                     listener.okData(rudderResponse == null ? new GroupGetListResponse() : responce);
                 }
             }

             @Override
             public void okData(RudderResponse rudderResponse, int fromWhere) {
                 super.okData(rudderResponse, fromWhere);


                 if (rudderResponse == null) {
                     listener.okData(new GroupGetListResponse(), fromWhere);
                 } else {
                     List<GroupGetList> items = gson.fromJson(rudderResponse.getBody(), type);
                     GroupGetListResponse responce = new GroupGetListResponse();
                     responce.setGetList(items);
                     listener.okData(rudderResponse == null ? new GroupGetListResponse() : responce, fromWhere);
                 }


             }

             @Override
             public void onFailure(int errorNo, String strMsg) {
                 super.onFailure(errorNo, strMsg);
                 listener.failue(errorNo, strMsg);
             }

             @Override
             public void onFinish() {
                 super.onFinish();
                 listener.Finish();
             }
         });
    }            
        
        

   
	
	
	/**
	 * 获取团购详情，根据是否参团显示不同内容，已参团显示“分享”和“现在下单”，同时显示参团人数，未参团显示“我要参团”和“我要开团”，团购分享内容为团购名称＋团购副图＋固定文字＋固定链接（一个静态介绍页面）
	 * @param request
	 * @param listener
	 * @return GroupGetResponse
	 */		
	 
	    public void get(GroupGetRequest request, final RemoteServiceListenerT<GroupGetResponse> listener) {

        final HttpParams params = new HttpParams();
        

            params.put("groupId",request.getGroupId());

        final String url = "group/get";


            new HttpService().get(context, url, params, GroupGetResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((GroupGetResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new GroupGetResponse() : (GroupGetResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new GroupGetResponse() : (GroupGetResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }
        });
    }

   
	
	
	/**
	 * 输入团购码获取团购信息，只有完全匹配的团购码才能获取到信息，无联想搜索功能
	 * @param request
	 * @param listener
	 * @return GroupGetByCodeResponse
	 */		
	 
	    public void getByCode(GroupGetByCodeRequest request, final RemoteServiceListenerT<GroupGetByCodeResponse> listener) {

        final HttpParams params = new HttpParams();
        

            params.put("code",request.getCode());

        final String url = "group/getByCode";


            new HttpService().get(context, url, params, GroupGetByCodeResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((GroupGetByCodeResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new GroupGetByCodeResponse() : (GroupGetByCodeResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new GroupGetByCodeResponse() : (GroupGetByCodeResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }
        });
    }

   
	
	
	/**
	 * 用户参加一个已经开的团，根据已经开团的id参加，返回开团的详细信息（同详情接口）
	 * @param request
	 * @param listener
	 * @return GroupJoinResponse
	 */		
	 
	    public void join(GroupJoinRequest request, final RemoteServiceListenerT<GroupJoinResponse> listener) {

        final HttpParams params = new HttpParams();
        

            params.put("instId",request.getInstId());

        final String url = "group/join";


            new HttpService().post(context, url, params, GroupJoinResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((GroupJoinResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new GroupJoinResponse() : (GroupJoinResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new GroupJoinResponse() : (GroupJoinResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }
        });
    }

   
	
	
	/**
	 * 用户开通一个新团，返回开团详情（同详细接口）
	 * @param request
	 * @param listener
	 * @return GroupOpenResponse
	 */		
	 
	    public void open(GroupOpenRequest request, final RemoteServiceListenerT<GroupOpenResponse> listener) {

        final HttpParams params = new HttpParams();
        

            params.put("groupId",request.getGroupId());

        final String url = "group/open";


            new HttpService().post(context, url, params, GroupOpenResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((GroupOpenResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new GroupOpenResponse() : (GroupOpenResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new GroupOpenResponse() : (GroupOpenResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }
        });
    }

   
	
	
	/**
	 * 用户已参加某团，可以退出，在参加同一个团多次只保留最后一个记录
	 * @param request
	 * @param listener
	 * @return GroupQuitResponse
	 */		
	 
	    public void quit(GroupQuitRequest request, final RemoteServiceListenerT<GroupQuitResponse> listener) {

        final HttpParams params = new HttpParams();
        

            params.put("groupId",request.getGroupId());

        final String url = "group/quit";


            new HttpService().post(context, url, params, GroupQuitResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((GroupQuitResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new GroupQuitResponse() : (GroupQuitResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new GroupQuitResponse() : (GroupQuitResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }
        });
    }

   
	
	
}